﻿using System;
using System.Globalization;
using Pantry.Entity;
using Pantry.Business;
namespace Pantry.Web.Users
{
    public partial class ViewListAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Postcode"] != null || Request.QueryString["Address"] != null || Request.QueryString["Town"] != null)
            {
                var address = new AddressSearch
                                  {
                                      Postcode = Request.QueryString["Postcode"],
                                      Address = Request.QueryString["Address"],
                                      Town = Request.QueryString["Town"]
                                  };
                CollectionPager.PageSize = 5;
                CollectionPager.BindToControl = RepeaterAddress;
                CollectionPager.DataSource =  new AddressBus().ReturnSearchValue(address);
                   
                if (CollectionPager.TotalRecords != 0)
                {
                    RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                    RepeaterAddress.DataBind();
                }
                else
                {

                    var dt =  new AddressBus().ListAddressesToDataSet().Tables[0];
                    CollectionPager.PageSize = 5;
                    CollectionPager.BindToControl = RepeaterAddress;
                    CollectionPager.DataSource = dt.DefaultView;
                    RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                    RepeaterAddress.DataBind();
                }

            }

            else
            {
                var dt =  new AddressBus().ListAddressesToDataSet().Tables[0];
                CollectionPager.PageSize = 5;
                CollectionPager.BindToControl = RepeaterAddress;
                CollectionPager.DataSource = dt.DefaultView;
                RepeaterAddress.DataSource = CollectionPager.DataSourcePaged;
                RepeaterAddress.DataBind();
            }
        }

        protected void AddressSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewListAddress.aspx?Postcode="+ txtPostcode.Text.Trim()+"&Address="+txtStreet.Text.Trim()+"&Town="+txtTown.Text.Trim()+"");
        }
        protected void None_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewListAddress.aspx");
        }
        protected void Clear_Click(object sender, EventArgs e)
        {
            txtPostcode.Text = null;
            txtStreet.Text = null;
            txtTown.Text = null;
        }
    }
}
